<?php

namespace app\common\utils;

class SysUtil
{
    public static function generateRouter(array $menus): array
    {
        $treeMap = array();
        $rootTree = array();

        foreach ($menus as $menu) {
            $treeMenu = [
                'menuId' => $menu['menuId'],
                'path' => $menu['path'],
                'component' => !empty($menu['component']) ? $menu['component'] : '',
                'name' => $menu['routeName'],
                'icon' => $menu['icon'],
                'visible' => $menu['visible'],
                'title' => $menu['menuName'],
                'parentId' => $menu['parentId'],
                'children' => null
            ];
            $treeMap[$menu['menuId']] = $treeMenu;
        }

        foreach ($menus as $menu) {
            $parentId = $menu['parentId'];
            $currentTreeMenu = &$treeMap[$menu['menuId']];

            if ($parentId == 0) {
                $rootTree[] = &$currentTreeMenu;
            } else {
                if (isset($treeMap[$parentId])) {
                    $parentTreeMenu = &$treeMap[$parentId];
                    if (!isset($parentTreeMenu['children'])) {
                        $parentTreeMenu['children'] = [];
                    }
                    $parentTreeMenu['children'][] = $currentTreeMenu;
                }
            }
        }
        return $rootTree;
    }

    public static function generateRoleMenuTree(array $menus): array
    {
        $treeMap = array();
        $rootTree = array();

        // 第一遍遍历：构建非叶子节点映射
        foreach ($menus as $menu) {
            if($menu['menuType'] != 'F'){
                $treeMenu = [
                    'menuId' => $menu['menuId'],
                    'parentId' => $menu['parentId'],
                    'menuName' => $menu['menuName'],
                    'children'=>[],
                    'perms'=>[]
                ];
                $treeMap[$menu['menuId']] = $treeMenu;
            }
        }

        // 第二遍遍历：构建父子关系
        foreach ($menus as $menu){
            if($menu['menuType'] == 'F'){
                continue;
            }

            $parentId = $menu['parentId'];
            $currentTreeMenu = &$treeMap[$menu['menuId']];

            if($parentId == 0){
                $rootTree[] = &$currentTreeMenu;
            }else{
                if(isset($treeMap[$parentId])){
                    $treeMap[$parentId]['children'][] = &$currentTreeMenu;
                }
            }
        }

        // 第三遍遍历：添加权限项
        foreach ($menus as $menu) {
            if($menu['menuType'] == 'F'){
                $parentId = $menu['parentId'];
                if(isset($treeMap[$parentId])){
                    $permItem = [
                        'menuId' => $menu['menuId'],
                        'parentId' => $menu['parentId'],
                        'menuName' => $menu['menuName']
                    ];
                    $treeMap[$parentId]['perms'][] = $permItem;
                }
            }
        }

        return $rootTree;
    }
}